<template>
  <el-container class="work-warp">
    <div class="top">
      <TopFilter ref="formPanel" :list="orderList" :cols="3" formType="add" @filterChange="getGdssList" />
    </div>
    <FilterTable
      ref="table"
      height="auto"
      maxHeight="350"
      :columns="columns"
      columnsRef="partPartOutboundRepairMaterialsComponentsWorkSearch"
      :fetchapi="fetchApi"
      :params="fetchParams"
      selectionType="single"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="arr => (this.onChangeColumn = arr)"
    ></FilterTable>
    <div class="footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" :loading="addBtnState" @click="saveDialog">确 定</el-button>
    </div>
  </el-container>
</template>
<script>
import { getGdssList, lockUser, getDealerUser } from '@/api/repair/repairReception/customer';
import { dictionary } from '@/utils/dictMixin';
import { getUserId } from '@/assets/js/auth';
import { isFormEmpty, arrayObjDeWeight } from '@/utils/index';
import { searchRoleByCode } from '@/api/common/empSelect/index';
import dictCode from '@/utils/dictCode';
import moment from 'moment';
import { state } from '../store';
import { queryWorkOrder } from '@/api/part/partOutbound/repairMaterials';

export default {
  name: 'workSearch',
  mixins: [dictionary],
  props: ['workNo'],
  data() {
    return {
      orderList: this.createFormList(),
      addBtnState: null,
      fetchApi: queryWorkOrder,
      fetchParams: { xhrAbort: true, completeTag: 10041002 },
      columns: this.createTableColumns(),
      onChangeColumn: [],
      userId: null
    };
  },
  watch: {
    // workNo: {
    //   handler(str) {
    //     this.orderList.some(item => {
    //       if (item.fieldName === 'roNo') {
    //         item.initialValue = str;
    //         return true;
    //       }
    //     });
    //   },
    //   immediate: true
    // }
  },

  mounted() {
    // console.log(state.dict_allUser);
    this.getUid();
    this.serviceAdvisor();
    // this.allPerson();
    if (this.workNo) {
      this.orderList.find(item => item.fieldName == 'roNo').initialValue = this.workNo;
      this.fetchParams = Object.assign(this.fetchParams, {
        xhrAbort: false,
        roNo: this.workNo
      });
    }
  },
  methods: {
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '工单号',
          fieldName: 'roNo',
          placeholder: ''
        },
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'license',
          placeholder: ''
        },
        {
          type: 'SELECT',
          label: '服务顾问',
          fieldName: 'serviceAdvisor',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '车主',
          fieldName: 'ownerName',
          placeholder: ''
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: ''
        },

        {
          type: 'INPUT',
          label: 'completeTag', //  completeTag: 10041002 // 查工单  不是竣工的（维修领料页面用）
          hidden: true,
          fieldName: 'completeTag',
          initialValue: '10041002'
        }
      ];
    },

    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '工单号',
          dataIndex: 'RO_NO',
          sorter: true,
          width: 130
        },
        {
          title: '工单类型',
          dataIndex: 'RO_TYPE',
          hidden: true,
          sorter: true,
          render: props => {
            return <span>{this.createDictText(props.row.RO_TYPE, '1253')}</span>;
          }
        },
        {
          title: '维修类型',
          dataIndex: 'REPAIR_TYPE_NAME',
          sorter: true,
          showOverflowTooltip: true
        },
        {
          title: '车牌号',
          dataIndex: 'LICENSE',
          sorter: true
        },
        // {
        //   title: '排量',
        //   dataIndex: 'EXHAUST_QUANTITY',
        //   filter: true,
        //   filterType: 'input'
        // },
        {
          title: 'VIN',
          dataIndex: 'VIN',
          width: 150,
          showOverflowTooltip: true,
          sorter: true
        },
        {
          title: '车主',
          dataIndex: 'OWNER_NAME',
          sorter: true,
          showOverflowTooltip: true
        },
        {
          title: '工单状态',
          dataIndex: 'RO_STATUS',
          sorter: true,
          render: props => {
            return <span>{this.createDictText(props.row.RO_STATUS, '8049')}</span>;
          }
        },
        {
          title: '服务顾问',
          dataIndex: 'SERVICE_ADVISOR',
          sorter: true,
          dictItems: []
        },
        {
          title: '开单日期',
          dataIndex: 'RO_CREATE_DATE',
          sorter: true,
          width: 140
          // render: props => {
          //   return <span>{props.row.RO_CREATE_DATE ? moment(props.row.RO_CREATE_DATE).format('YYYY-MM-DD hh:mm:ss') : ''}</span>;
          // }
        },

        // {
        //   title: '锁定人',
        //   dataIndex: 'LOCK_USER',
        //   sorter: true,
        //   dictItems: state.dict_allUser
        // },
        {
          title: '竣工标识',
          dataIndex: 'COMPLETE_TAG',
          sorter: true,
          render: props => {
            return <span>{this.createDictText(props.row.COMPLETE_TAG, '1004')}</span>;
          }
        },
        {
          title: '预交车时间',
          dataIndex: 'END_TIME_SUPPOSED',
          sorter: true,
          render: props => {
            return <span>{props.row.END_TIME_SUPPOSED ? moment(props.row.END_TIME_SUPPOSED).format('YYYY-MM-DD hh:mm:ss') : ''}</span>;
          }
        }
      ];
    },

    formChangeHandle() {},

    query() {
      this.$refs.formPanel.SUBMIT_FORM();
    },

    closeDialog() {
      this.$emit('closeDialog', false);
    },

    async saveDialog() {
      if (this.onChangeColumn.length) {
        this.$emit('saveDialog', this.onChangeColumn);
        // let obj = this.onChangeColumn[0];
        // if (obj.LOCK_USER) {
        //   if (obj.LOCK_USER != this.getUserId) {
        //     this.$notify({
        //       title: '提示消息',
        //       message: `此单号${obj.RO_NO}已被用户${obj.LOCK_USER}锁定!`,
        //       type: 'warning'
        //     });
        //     return;
        //   } else {
        //     this.$emit('saveDialog', this.onChangeColumn);
        //   }
        // } else {
        //   let objGo = {
        //     tabName: 'TT_REPAIR_ORDER',
        //     field: 'RO_NO',
        //     val: obj.RO_NO
        //   };
        //   let res = await lockUser(objGo);
        //   this.$emit('saveDialog', this.onChangeColumn);
        // }
      }
    },
    getUid() {
      this.getUserId = getUserId();
      console.log('this.getUserId', this.getUserId);
    },
    getGdssList(val) {
      this.fetchParams.xhrAbort = true;
      this.fetchParams = Object.assign({}, val, { completeTag: 10041002, isflag: 1001, xhrAbort: false });
    },
    // 查询服务顾问列表
    async serviceAdvisor() {
      let testDrive = { role: dictCode.SERVICE_CONSULTANT };
      const res = await searchRoleByCode(testDrive);
      let data = arrayObjDeWeight(res.data, 'userId'); // 人员去重(解决前端报错)
      this.orderList.find(item => item.fieldName == 'serviceAdvisor').itemList = data.map(x => ({
        text: x.employeeName,
        value: x.userId + ''
      }));
      this.columns.find(x => x.dataIndex === 'SERVICE_ADVISOR').filterItems = this.columns.find(x => x.dataIndex === 'SERVICE_ADVISOR').editItems = data.map(x => ({
        text: x.employeeName,
        value: x.userId + ''
      }));
      // this.columns.find(x => x.dataIndex === 'SERVICE_ADVISOR').filterItems = this.columns.find(x => x.dataIndex === 'SERVICE_ADVISOR').editItems = this.columns.find(
      //   x => x.dataIndex === 'LOCK_USER'
      // ).filterItems = this.columns.find(x => x.dataIndex === 'LOCK_USER').editItems = data.map(x => ({
      //   text: x.employeeName,
      //   value: x.userId + ''
      // }));
    },
    // 查询所有员工
    async allPerson() {
      const res = await getDealerUser();
      let data = arrayObjDeWeight(res.data, 'userId'); // 人员去重(解决前端报错)
      // this.columns.find(x => x.dataIndex === 'LOCK_USER').filterItems = this.columns.find(x => x.dataIndex === 'LOCK_USER').editItems = data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
    },

    reset() {
      this.$refs.formPanel.RESET_FORM();
    }
  }
};
</script>
<style lang="less" scoped>
.work-warp {
  flex-direction: column;

  .top {
    width: 100%;
    display: flex;
    flex-direction: column;

    .el-row {
      text-align: center;
    }
  }

  .footer {
    position: absolute;
    left: 0px;
    bottom: 0px;
    right: 0px;
    z-index: 9;
    border-top: 1px solid rgb(233, 233, 233);
    padding: 10px 20px;
    background: rgb(255, 255, 255);
    text-align: right;
  }
}
</style>
